package com.mcxx.modules.minimumliving.service.impl;

import com.alibaba.fastjson.JSON;
import com.ctop.crypt.sm.SM3Digest;
import com.ctop.crypt.sm.sm4.SM4Coder;
import com.mcxx.modules.minimumliving.service.FiscalAbutmentService;
import com.mcxx.modules.miniSalvation.family.dto.BankAbutmentDTO;
import com.mcxx.util.JsonUtil;
import com.mcxx.util.OkHttpUtil;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@TaTransactional
public class FiscalAbutmentServiceImpl implements FiscalAbutmentService {
    @Value("${abutment.http.root-url:http://ys.hztech.cn:8888}")
    private String rootUrl;
    @Value("${abutment.http.qryObjAccount:/rescueSys/targetQuery/qryObjAccount}")
    private String qryObjAccount;
    @Value("${abutment.http.login:/rescueSys/login}")
    private String login;
    @Value("${abutment.http.loginSecurityKey:Pzi4g3N0S7igJxbz}")
    private String loginSecurityKey;
    @Value("${abutment.http.otherSecurityKey:s6siQXovrE5TRD8c}")
    private String otherSecurityKey;
    @Value("${abutment.http.userName:test0001}")
    private String userName;
    @Value("${abutment.http.passWord:123456}")
    private String passWord;
    @Value("${abutment.http.datasources:01}")
    private String datasources;

    private static SM4Coder sm4 = new SM4Coder();
    @Override
    public String fiscalAbutmentInfo(BankAbutmentDTO dto) {

        if(this.isTokenExpired()){
            this.loginAbutment();
        }

        String result = this.qryObjAccount(dto);

        return result;
    }

    public String loginAbutment() {
        String url = rootUrl+login;
        Map<String, Object> bodys = new HashMap<String, Object>();
        bodys.put("userName", userName);
        bodys.put("passWord", passWord);
        bodys.put("datasources", datasources);

        String formBody = JSON.toJSONString(bodys);
        String crc = SM3Digest.getCrcCode(formBody);
        String body = "";
        try {
            sm4.setSecretKey(loginSecurityKey);
            body = sm4.encryptData_ECB(formBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = OkHttpUtil.doPost(url,body,null,"login",crc);



//            loginResult = objectMapper.readValue(result, Result.class);



        return result;
    }


    public String qryObjAccount(BankAbutmentDTO dto){
        String url = rootUrl+qryObjAccount;

        String formBody = JsonUtil.objectToJson(dto);
        String crc = SM3Digest.getCrcCode(formBody);
        String body = "";
        try {
            sm4.setSecretKey(otherSecurityKey);
            body = sm4.encryptData_ECB(formBody);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = OkHttpUtil.doPost(url,body,null,"qryObjAccount",crc);
        return result;
    }





    private Boolean isTokenExpired(){
        Date now = new Date();
        if("".equals(OkHttpUtil.ABUTMENT_TOKEN)){
            OkHttpUtil.beginDate = now;
            return true;
        }
        if(OkHttpUtil.beginDate != null){
            long expiredMintes =getTime(now,OkHttpUtil.beginDate);
            if(expiredMintes >= 5){
                OkHttpUtil.beginDate = now;
                return true;
            }
        }
        return false;
    }


    public static long getTime(Date newTime,Date oldTime){
        long NTime = newTime.getTime();
        long OTime = oldTime.getTime();
        long diff = (NTime-OTime)/1000/60;
        return diff;

    }



}
